﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Peekaboo2008.model;
using Peekaboo2008.dao;

namespace Peekaboo2008.view
{
    public partial class FormChiTietLoaiCongNo : Form
    {
        private LoaiCongNoModel loaiCongNoModel = null;
        private FormLoaiCongNo formLoaiCongNo;
        private FormChiTietCongNo formChiTietCongNo;
        private Int32 flag = 0;
        public FormChiTietLoaiCongNo()
        {
            InitializeComponent();
        }
        public FormChiTietLoaiCongNo(LoaiCongNoModel _loaiCongNoModel, ref FormLoaiCongNo _formLoaiCongNo)
        {
            InitializeComponent();
            formLoaiCongNo = _formLoaiCongNo;
            loaiCongNoModel = _loaiCongNoModel;
            txtTenLoaiCongNo.Text = loaiCongNoModel.Ten;
            txtGhiChu.Text = loaiCongNoModel.Ghi_chu;        
        }
        public FormChiTietLoaiCongNo(ref FormLoaiCongNo _formLoaiCongNo)
        {
            InitializeComponent();
            txtTenLoaiCongNo.Focus();
            formLoaiCongNo = _formLoaiCongNo;
        }
        public FormChiTietLoaiCongNo(ref FormChiTietCongNo _formChiTietCongNo)
        {
            InitializeComponent();
            formChiTietCongNo = _formChiTietCongNo;
            txtTenLoaiCongNo.Focus();
            flag = 1;        
        }

        private void btnKetThuc_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnChapNhan_Click(object sender, EventArgs e)
        {
            if (txtTenLoaiCongNo.Text.Equals(""))
            {
                MessageBox.Show("Tên loại công nợ không được bỏ trống !");
                txtTenLoaiCongNo.Focus();
            }
            else
            {
                if (loaiCongNoModel == null)
                {
                    loaiCongNoModel = new LoaiCongNoModel();
                }
                loaiCongNoModel.Ten = txtTenLoaiCongNo.Text;                
                loaiCongNoModel.Ghi_chu = txtGhiChu.Text;
                

                if (loaiCongNoModel.ID == null || loaiCongNoModel.ID == "")
                {
                    if (LoaiCongNoDAO.InsertLoaiCongNo(loaiCongNoModel))
                    {
                        if (flag == 1)
                        {
                            formChiTietCongNo.FormChiTietCongNoLoad();
                        }
                        else
                        {
                            formLoaiCongNo.FormLoaiCongNoLoad();
                        }
                        MessageBox.Show("Thêm mới loại công nợ thành công","Thông báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
                        this.Dispose();
                    }
                    else
                    {
                        MessageBox.Show("Thêm mới loại công nợ thất bại","Lỗi",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    }
                }
                else
                {
                    if (LoaiCongNoDAO.UpdateLoaiCongNo(loaiCongNoModel))
                    {
                        formLoaiCongNo.FormLoaiCongNoLoad();
                        MessageBox.Show("Cập nhật loại công nợ Thành Công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Dispose();
                        
                    }
                    else
                    {
                        MessageBox.Show("Cập nhật loại công nợ thất bại","Lỗi",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    }                    
                }
                this.Dispose();
            }
        }

        private void FormChiTietLoaiCongNo_Load(object sender, EventArgs e)
        {
            this.KeyPreview = true;
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FormChiTietLoaiCongNo_KeyDown);
        }

        private void FormChiTietLoaiCongNo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F8)
            {
                btnChapNhan.PerformClick();
            }           
            if (e.KeyCode == Keys.F12)
            {
                this.Dispose();
            }
        }
    }
}
